package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	import spark.components.Label;

	public class VDD extends EventDispatcher
	{
		private var key:int;
		private var btkey : int;
//		private var imgArray:Array = new Array();
		private var loaderArray:Array = new Array();		
//		private var typeArray:Array = new Array();
//		private var sizeArray:Array = new Array();		
		private var ImgInfoArray : Array = new Array();
		
		private var mDirCount:int=0;
		private var mKeyFrameCount:int = 0;
		
		public var loaded:Boolean = false;
		public var released:Boolean = false;//wk 已经释放的，需要重新loader加载
		
		public var url:String = "";
		public var mCount:int = 0;
		public var lastUseTime:int = getTimer();
		public var mRefCount:int  =0 ;//引用计数
		
		public static var importRoot:String;
		public static var exportUrl:String;
		public static var isJPG:Boolean;
		public static var isSC:Boolean;
		public static var isSplit:Boolean = false;
		public static var pinzhi:int = 80;
		public static var mCfgStream:FileStream = new FileStream();
		public static var mBmdStream:FileStream = new FileStream();
		public static var mBuffers:Array = [];
		public static var mTimer:Timer;
		public static var mDelay:int;
		public static var promptMsg:Label;
		public static var packCanvas:Canvas;
		public static var packTemp:*;
		public static var packUI:UIComponent = new UIComponent();
		public static var _testTime:int;
		public static var _testCount:int=1;
		public static var msgtext:String="";
		public static var printMsg:Label;
		public static var fileCount:int;
		public static var fileNum:int;
		public static var resType:int = 0;//0为vdd, 1为json
		
		public var _cfgInit:Boolean = false;
		
		public function VDD()
		{
			if(!mTimer)
			{
				mTimer = new Timer(16);
				mTimer.addEventListener(TimerEvent.TIMER, onTimerEvent);
				mTimer.start();
			}
		}
		private static function onTimerEvent(e:TimerEvent):void
		{
			var l3:*;
			var l5:int;
			if(_testTime == 0)
			{
				_testTime = new Date().time;
			}
			else
			{
				l5 = new Date().time;
				if(l5 -_testTime < 100)
				{
					_testCount ++;
				}
				else
				{
					_testCount --;
					if(_testCount < 1)
					{
						_testCount = 1;
					}
				}
				_testTime = l5;
			}
			var l2:Array = [];
			var l4:int;
			while(l4 < _testCount)
			{
				if(mBuffers.length)
				{
					l2.push(mBuffers.shift());
				}
				else
				{
					break;
				}
				l4++;
			}
			l4 = 0;
			while(l4 < l2.length)
			{
				writeFiles(l2[l4++]);
			}
		}
		public function TheInit(data:ByteArray):void 
		{
			parser(data);
			return;
			
			//------以下是镇江微端--------//
			data.endian = Endian.LITTLE_ENDIAN;
			
			var arrPtr:Array = [];
			//var vddhead:String = "";
			var num:int;
			var ver:int;
			var i:int;
			var j :int;
			var k :int;
			
			var type:int;
			var size:int;
			var px:int;
			var py:int;
			var tmp :int;
			
			var frameCountEnd:int = 0;
			
			//vddhead = data.readUTFBytes(10);	//
			data.position = 40;		//title
			num = data.readInt();	//num
			ver = data.readInt();			//ver
			key = ((num | ver) << 16) | (num & 0xFFFF);
			btkey = (num | ver) & 0xFF;
			mCount = num;
			
			for(i = 0; i < num; ++i)
				arrPtr.push(data.readInt());
			
			var base:int = 48 + num * 4;
			
			var dec:Loader;
			for(i = 0; i < num; ++i)
			{

				data.position = base + (arrPtr[i] ^ key);
				
				type = DecodeInt(data.readByte()) & 0xFF;
				data.readByte();
				data.readByte();
				data.readByte();
				size = DecodeInt(data.readInt());
				px = DecodeInt(data.readShort() << 16) >> 16;
				py = DecodeInt(data.readShort() << 16) >> 16;
				 
				var imginfo : VDDImageInfo = new VDDImageInfo();
				imginfo.parentVDD = this;
				imginfo.idx = i;
				imginfo.type = type;
				imginfo.x = px;
				imginfo.y = py;
				
				ImgInfoArray[i] = imginfo;
				
//				sizeArray[i] = size;
				
				if(size < 10)
				{
//					imgArray[i] = null;
					frameCountEnd = 1;
					continue;
				}else {
					mKeyFrameCount++;
				}
				
				if(i % 10 == 0) mDirCount++;
				
				var buf:ByteArray = new ByteArray;
				data.readBytes(buf, 0, size);
				
				if (buf.length > 12)
				{
					for (k=0;k<12;k++)
					{
						buf[k] = (buf[k] ^ btkey) & 0xFF;
					}
				}
				buf.position = 0;
				
				
				dec = new Loader();
				
				loaderArray[i] = dec;
				
				dec.name = i + "";
				dec.contentLoaderInfo.addEventListener(Event.COMPLETE, handleDecodeDone);
				dec.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleDecodeError);
				dec.loadBytes(buf);
				
			}
			if(!dec)
			{
				fileCount ++;
			}
			
			loaded = true;
			
		}
		
		private function DecodeInt(src :int):int
		{
			var intbtkey:int = (btkey << 24) | (btkey << 16) | (btkey << 8) | btkey;
			return src ^ intbtkey;			
		}
		
		public function TheDest():void
		{
			for each(var info:VDDImageInfo in ImgInfoArray)
			{
				info.theDest();
			}
			ImgInfoArray.length = 0;
			
			for each (var l:Loader in loaderArray) 
			{
				if(l) {
					l.contentLoaderInfo.removeEventListener(Event.COMPLETE,handleDecodeDone);
					l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleDecodeError);
					l.unload();
				}
			}
			loaderArray = null;		
			released = true;
			
			packs = null;
			if(packBmds)
			{
				var i:int;
				for( i=0; i<packBmds.length; i++)
				{
					packBmds[i].dispose();
				}
			}
			packBmds = null;
			packList = null;
			
			var vddmap:Dictionary = LoaderVDD.instance().mVDDMap;
			delete LoaderVDD.instance().mVDDMap[url];
		}
		
		public var jsonData:String;
		public var jsonObj:Object;
		/**
		 * 拆分图片
		 */
		public function TheJson(data:String):void
		{
			if(data.indexOf("packNum:") == 0)
			{
				fileCount ++;
				return;
			}
			jsonData = data;
			var pngurl:String = url.replace(".json", ".png");
			var dec:Loader = new Loader();
			dec.contentLoaderInfo.addEventListener(Event.COMPLETE, handlePngComplete);
			dec.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handlePngError);
			dec.load(new URLRequest(pngurl));
		}
		private function handlePngComplete(e:Event):void
		{
			var li:LoaderInfo = e.target as LoaderInfo;
			
			var id:int = int(li.loader.name);
			var srcBD:BitmapData = Bitmap(li.content).bitmapData;
			li.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,handleDecodeDone);
			li.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleDecodeError);
			li.loader.unload();
			
			var size2:int = 2048;
			if(!VDD.isSplit && srcBD.width * srcBD.height < size2 * size2)
			{
				fileCount ++;
				freshTips();
				return;
			}
			
			var str:String = jsonData;
			var obj:Object = JSON.parse(str);
			jsonObj = obj;
			for(var key:* in obj)
			{
				var imginfo : VDDImageInfo = new VDDImageInfo();
				imginfo.parentVDD = this;
				imginfo.idx = key;
				imginfo.type = obj[key].type;
				imginfo.x = obj[key].ox;
				imginfo.y = obj[key].oy;
				var bd:BitmapData = new BitmapData(obj[key].w, obj[key].h, true, 0x00000000);
				bd.copyPixels(srcBD, new Rectangle(obj[key].x, obj[key].y, obj[key].w, obj[key].h), new Point());
				
				imginfo.imageData = bd;
				imginfo.vddUrl = this.url;
				imginfo.width = bd.width;
				imginfo.height = bd.height;
				
				var curSize:int = imginfo.width * imginfo.height;
				packSize += curSize;
				packList.push({size:curSize, imginfo:imginfo});
			}
			handleWrites();
		}
		private function handlePngError(e:IOErrorEvent):void
		{
			trace("png加载失败：" + url);
		}
		//----------------奇侠  资源打包 top----------------------//
		public static function byteToBitmap(bytes:ByteArray, uncompress:Boolean=true):BitmapData
		{
			if (uncompress)
			{
				bytes.uncompress();
			};
			bytes.position = 0;
			var width:int = bytes.readUnsignedInt();
			var height:int = (((bytes.length - 4) / 4) / width);
			var bmd:BitmapData = new BitmapData(width, height, true, 0);
			bmd.setPixels(bmd.rect, bytes);
			return (bmd);
		}
		
		public function parser(bytes:ByteArray):void
		{
			var img:* = null;
			var bmdByte:* = null;
			var i:int;
			bytes.position = (bytes.length - 3);
			var key:String = bytes.readMultiByte(3, "utf-8");
			if (key != "YER")
			{
				bytes.uncompress();
				bytes.position = bytes.length;
				bytes.writeMultiByte("YER", "utf-8");
			};
			bytes.position = 0;
			var memory:* = 0;
			var blendMode:String = bytes.readUTF();
			var length:int = bytes.readByte();
			i = 0;
			while (i < length)
			{
				mKeyFrameCount++;
				
				var imginfo : VDDImageInfo = new VDDImageInfo();
				imginfo.parentVDD = this;
				imginfo.idx = i;
				imginfo.x = bytes.readShort();
				imginfo.y = bytes.readShort();
				imginfo.delayTime = bytes.readInt();
				imginfo.imageName = bytes.readUTF();
				imginfo.x = (imginfo.x - (bytes.readShort() * 0.5));
				imginfo.y = (imginfo.y - (bytes.readShort() * 0.5));
				imginfo.blendMode = blendMode;
				
				bmdByte = new ByteArray();
				bytes.readBytes(bmdByte, 0, bytes.readInt());
				var bmd:BitmapData = byteToBitmap(bmdByte);
				imginfo.imageData = bmd;
				imginfo.vddUrl = this.url;
				ImgInfoArray[i] = imginfo;
				
				i++;
			};
			
			for(var id:* in ImgInfoArray)
			{
				makeNewBitmap(ImgInfoArray[id]);
			}
		}
		//----------------奇侠  资源打包 end----------------------//
		private function handleDecodeDone(e:Event):void
		{
			var li:LoaderInfo = e.target as LoaderInfo;
			
			var id:int = int(li.loader.name);
            var srcBD:BitmapData = Bitmap(li.content).bitmapData;
			
			li.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,handleDecodeDone);
			li.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleDecodeError);
			li.loader.unload();
			li = null;
			loaderArray[id] = null;
			delete loaderArray[id];
			var info:VDDImageInfo = ImgInfoArray[id] as VDDImageInfo;
			info.imageData = srcBD;
			info.vddUrl = this.url;
			if (srcBD)
			{
				info.width = srcBD.width;
				info.height = srcBD.height;
			}
			delete ImgInfoArray[id];
			
			makeNewBitmap(info);
		}
		private function makeNewBitmap(info:VDDImageInfo):void
		{
			//解析
			var curSize:int = info.width * info.height;
			packSize += curSize;
			packList.push({size:curSize, id:info.idx, imginfo:info});
			packCount ++;
			if(packCount >= this.mKeyFrameCount)
			{
				handleWrites();
			}else
			{
				//throw new Error("出错！makeNewBitmap");
			}
		}
		/**
		 * 最终写入文件
		 */
		private function handleWrites():void
		{
//			this.packList.sortOn("size", Array.NUMERIC);
//			this.packList.reverse();
			this.packList.sortOn("id", Array.NUMERIC);
			this.packList.reverse();
			if(!this.packs)
			{
				//					var size1:int = 1024;
				var size2:int = 2048;
				//					var size3:int = 4096;
				packs = new Vector.<MaxRectsBinPack>();
				
				packs.push(new MaxRectsBinPack(size2, size2, false));
				if(packSize > size2 * size2 || VDD.isSplit)
				{
					packs.push(new MaxRectsBinPack(size2, size2, false));
//					packs.push(new MaxRectsBinPack(size2, size2, false));
//					packs.push(new MaxRectsBinPack(size2, size2, false));
				}
				//					else if(packSize > size1 * size1 * 0.8)
				//					{
				//						pack = new MaxRectsBinPack(size2, size2, false);
				//					}
				//					else
				//					{
				//						pack = new MaxRectsBinPack(size1, size2, false);
				//					}
				
				packBmds = new Vector.<BitmapData>();
			}
			var i:int = 0;
			var bmd:BitmapData;
			var rct:Rectangle;
			var curw:int;
			var curh:int;
			var maxw:int;
			var maxh:int;
			var n:int = 0;
			var rctObj:Object = {};
			var rctArr:Array;
			rctArr = [];
			
			for( n=0; n<packs.length; n++)
			{
				var pack:MaxRectsBinPack = packs[n];
				maxw = 0;
				maxh = 0;
				var len:int = Math.floor(packList.length / packs.length);
				i = len * n;
				var end:int = i + len;
				
				end = packList.length;
				i = packPoint;
				var temp:int = i;
				
				if(n == 0 && VDD.isSplit)
				{
					end = Math.floor(packList.length / 2);
				}
				if(i >= end)
				{
					break;
				}
				while(i < end)
				{
					bmd = packList[i].imginfo.imageData;
					rct = pack.insert(bmd.width, bmd.height, 3);
					curw = rct.x + rct.width;
					curh = rct.y + rct.height;
					if(rct.width == 0 || rct.height == 0)
					{
						packPoint = i;
						if(n >= packs.length - 1)
						{
							packs.push(new MaxRectsBinPack(size2, size2, false));
						}
						break;
					}
					if(i == end - 1)
					{
						packPoint = end;
					}
					if(curw > maxw)
					{
						maxw = curw;
					}
					if(curh > maxh)
					{
						maxh = curh;
					}
					rctArr.push(rct);
					if(i == packList.length - 1)
					{
						this.writeCfgs(packList[i].imginfo, rct, true, n);
					}
					else
					{
						this.writeCfgs(packList[i].imginfo, rct, false, n);
					}
					
					i ++;
				}
				
				
				
				packBmds[n] = new BitmapData(maxw, maxh, true, 0);//新图片
				i = temp;
				end = packPoint;
				if(n == 0 && VDD.isSplit)
				{
					end = Math.floor(packList.length / 2);
				}
				while(i < end)
				{
					rct = rctArr[i];
					bmd = packList[i].imginfo.imageData;
					var curRct:Rectangle = new Rectangle(0, 0, rct.width, rct.height);
					var pos:Point = new Point(rct.x, rct.y);
					packBmds[n].copyPixels(bmd, curRct, pos);
					i++;
				}
				
				timervdds.push({vdd:this, index:n});
				if(!timer)
				{
					timer = new Timer(33);
					timer.addEventListener(TimerEvent.TIMER, imgTimer);
					timer.start();
				}
			}
			
			
			if(!packTemp)
			{
				packTemp = packBmds;
			}
		}
		
		private static var _curTime:int = 0;
		private static function imgTimer(e:TimerEvent):void
		{
			if(timervdds.length == 0)
			{
				return;
			}
			var nowtime:int;
			if(_curTime == 0)
			{
				_curTime = new Date().time;
			}
			else
			{
				nowtime = new Date().time;
				if(nowtime - _curTime > 36)
				{
					_curTime = nowtime;
					return;
				}
				_curTime = nowtime;
			}
			var obj:Object = timervdds.shift();
			var vdd:VDD = obj.vdd;
			var index:int = obj.index;
			
			if(index >= vdd.packBmds.length-1)
			{
				fileCount ++;
			}
			vdd.writeImg(vdd.packBmds[index], index);
			
			if(index >= vdd.packBmds.length-1)
			{
				vdd.TheDest();
				LoaderVDD.instance().dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		public var packPoint:int = 0;
		public var packIndex:int = 0;
		public static var timer:Timer;
		public static var timervdds:Array = [];
		public var packBmds:Vector.<BitmapData>;
		public var packs:Vector.<MaxRectsBinPack>;
		public var packSize:int = 0;
		public var packCount:int;
		public var packList:Array = [];
		public var packDouble:Boolean = false;//是否分多个图片输出
		
		public function writeImg(param1:BitmapData, index:int=0):void
		{
			var imgByteArray:ByteArray;
			var path:String;
			var jpgenc:JPEGEncoder;
			var pngenc:PNGEncoder;
			var msg:Label;
			var bmd:BitmapData = param1;
			var laststr:String;
			if(resType == 1)//从json拆分
			{
				for each(var obj:* in jsonObj)
				{
					if(obj.type == 2)//黑底
					{
						jpgenc = new JPEGEncoder(VDD.pinzhi);
						imgByteArray = jpgenc.encode(bmd);
					}
					else//透明
					{
						pngenc = new PNGEncoder();
						imgByteArray = pngenc.encode(bmd);
					}
					break;
				}
			}
			else
			{
				if(VDD.isJPG)
				{
					jpgenc = new JPEGEncoder(VDD.pinzhi);
					imgByteArray = jpgenc.encode(bmd);
				}
				else
				{
					pngenc = new PNGEncoder();
					imgByteArray = pngenc.encode(bmd);
				}
			}
			if(VDD.isJPG && VDD.isSC)
			{
				laststr = "";
			}
			else
			{
				laststr = ".png";
				if(index > 0)
				{
					laststr = "~"+index+".png";
				}
			}
			var url:String = this.url;
			url = url.replace(".json", "");//如果是拆分图片，则还原成例如：e32.vdd，而不是e32.vdd.json
			var startIndex:int = url.lastIndexOf("/");
			var endIndex:int = url.length;
			var dic2:String = url.substring(startIndex+1, endIndex);
			var dicStart:int = importRoot.lastIndexOf("/");
			var dicEnd:int = startIndex;
			var dic:String = url.substring(dicStart+1, dicEnd);
			if(dic == "")
			{
				path = dic + dic2 + laststr;
			}
			else
			{
				path = dic + "/" + dic2 + laststr;
			}
			if(VDD.isJPG && VDD.isSC)
			{
				path = path.replace(".vdd", ".sc");
			}
			var rootFile:File = new File(exportUrl);
			var fl:File = rootFile.resolvePath(path);
			var fs:FileStream = mBmdStream;
			try
			{
//				if(fl.exists){
//					fs.open(fl, FileMode.UPDATE);
//				}
//				else{
					fs.open(fl, FileMode.WRITE);
//				}
				fs.writeBytes(imgByteArray);
				fs.close();
				msg = VDD.printMsg;
				freshTips();
			}
			catch(e:Error)
			{
				throw new Error("打包出错：" + path);
			}
		}
		private function freshTips():void
		{
			var str:String = url.substring(url.length/2);
			if(fileCount >= fileNum)
			{
				msgtext = fileCount + "/" + fileNum + "  解包完成！源文件：" + str;
			}
			else
			{
				msgtext = fileCount + "/" + fileNum + "  正在解包，源文件：" + str;
			}
			printMsg.text = msgtext;
		}
		public static function get running():Boolean
		{
			return !(fileCount >= fileNum);
		}
		public function writeCfgs(param1:VDDImageInfo, param2:Rectangle, param3:Boolean =false, from:int=0):void
		{
			var path:String;
			var txtStr:String;
			var hz:String;
			var info:VDDImageInfo = param1;
			var rct:Rectangle = param2;
			var isEnd:Boolean = param3;
			if(VDD.isJPG && VDD.isSC)
			{
				return;
			}
			var url:String = info.vddUrl;
			url = url.replace(".json", "");//如果是拆分图片，则还原成例如：e32.vdd，而不是e32.vdd.json
			var startIndex:int = url.lastIndexOf("/");
			var endIndex:int = url.length;
			var dic2:String = url.substring(startIndex+1, endIndex);
			var dicStart:int = importRoot.lastIndexOf("/");
			var dicEnd:int = startIndex;
			var dic:String = url.substring(dicStart+1, dicEnd);
			if(dic == "")
			{
				path = dic + dic2 + ".json";
			}
			else
			{
				path = dic + "/" + dic2 + ".json";	
			}
			var rootFile:File = new File(exportUrl);
			var fl:File = rootFile.resolvePath(path);
			var fs:FileStream = mCfgStream;
			try
			{
				if(info.parentVDD._cfgInit)
				{
					mCfgStream.open(fl, FileMode.APPEND);
				}
				else
				{
					info.parentVDD._cfgInit = true;
					mCfgStream.open(fl, FileMode.WRITE);
					mCfgStream.writeUTFBytes("{");
				}
				
//				imginfo.idx = i;
//				imginfo.x = bytes.readShort();
//				imginfo.y = bytes.readShort();
//				imginfo.delayTime = bytes.readInt();
//				imginfo.imageName = bytes.readUTF();
//				imginfo.x = (imginfo.x - (bytes.readShort() * 0.5));
//				imginfo.y = (imginfo.y - (bytes.readShort() * 0.5));
//				imginfo.blendMode = blendMode;
				
				txtStr = "\"" + info.idx + "\":{\"bm\":\"" + info.blendMode + "\",\"dt\":" + info.delayTime +",\"in\":\"" + info.imageName +"\",\"f\":" + from + ",\"ox\":" + info.x + ",\"oy\":" + info.y + ",\"x\":" + rct.x + ",\"y\":" + rct.y + ",\"w\":" + rct.width + ",\"h\":" + rct.height + "}";
				if(isEnd)
				{
					hz = "}" + "packNum:" + packs.length;
				}
				else
				{
					hz = ",";
				}
				txtStr = txtStr + hz;
				mCfgStream.writeUTFBytes(txtStr);
				mCfgStream.close();
			}
			catch(e:Error)
			{
				
			}
		}
		public static function writeFiles(param1:VDDImageInfo):void
		{
//			var imgByteArray:ByteArray;
//			var jpgenc:JPEGEncoder;
//			var pngenc:PNGEncoder;
//			var txtName:String;
//			var txtStr:String;
//			var info:VDDImageInfo = param1;
//			var url:String = info.vddUrl;
//			var srcBD:BitmapData = info.imageData;
//			var laststr:String;
//			if(VDD.isJPG)
//			{
//				laststr = "";
//				jpgenc = new JPEGEncoder(80);
//				imgByteArray = jpgenc.encode(srcBD);
//			}
//			else
//			{
//				pngenc = new PNGEncoder();
//				imgByteArray = pngenc.encode(srcBD);
//			}
//			var startIndex:int = url.lastIndexOf("/");
//			var endIndex:int = url.length;
//			var dic2:String = url.substring(startIndex, endIndex);
//			var fname:String = dic2;
//			startIndex = fname.lastIndexOf(".");
//			fname = fname.substring(0, startIndex) + "-" + 
			
		}
		
		private function handleDecodeError(e:ErrorEvent):void
		{
		}
		
		public function getImage(id:int):BitmapData
		{
			lastUseTime = getTimer();
			if(ImgInfoArray[id]) {
				return (ImgInfoArray[id]  as VDDImageInfo).imageData;
			}
			return null;
		}
		
		public function getPx(id:int):int
		{
			lastUseTime = getTimer();
			if(ImgInfoArray[id]) {
				return (ImgInfoArray[id] as VDDImageInfo).x;
			}
			return 0;
		}
		
		public function getPy(id:int):int
		{
			lastUseTime = getTimer();
			if(ImgInfoArray[id]) {
				return (ImgInfoArray[id] as VDDImageInfo).y;
			}
			return 0;
		}
		
		public function getVDDImageInfo(idx : int): VDDImageInfo
		{
			lastUseTime = getTimer();
			return ImgInfoArray[idx];
		}
		
		public function getDirCount():int
		{
			lastUseTime = getTimer();
			return mDirCount;
		}
		
		public function getFrameCount():int
		{
			lastUseTime = getTimer();
			return mKeyFrameCount;
		}		
		
		public function getWidth(idx:int) :int
		{
			lastUseTime = getTimer();
			if(ImgInfoArray[idx]) {
				var info:VDDImageInfo = ImgInfoArray[idx];
				if (info && info.imageData)
				{
					return info.imageData.width;
				}
			}
			return 0;
		}
		public function getHeight(idx:int) :int
		{
			lastUseTime = getTimer();
			if(ImgInfoArray[idx]) {
				var info:VDDImageInfo = ImgInfoArray[idx];
				if (info && info.imageData)
				{
					return info.imageData.height;
				}
			}
			return 0;
		}
		
	}
	
	
	
}